package com.ruoyi.api.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DzZaixian;
import com.ruoyi.system.service.IDzZaixianService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 在线记录Controller
 * 
 * @author ruoyi
 * @date 2022-09-28
 */
@RestController
@RequestMapping("/api/zaixian")
public class DzZaixianApiController extends BaseController
{
    @Autowired
    private IDzZaixianService dzZaixianService;

    /**
     * 查询在线记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DzZaixian dzZaixian)
    {
        startPage();
        List<DzZaixian> list = dzZaixianService.selectDzZaixianListWeb(dzZaixian);
        return getDataTable(list);
    }

    /**
     * 导出在线记录列表
     */
    @Log(title = "在线记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DzZaixian dzZaixian)
    {
        List<DzZaixian> list = dzZaixianService.selectDzZaixianListWeb(dzZaixian);
        ExcelUtil<DzZaixian> util = new ExcelUtil<DzZaixian>(DzZaixian.class);
        util.exportExcel(response, list, "在线记录数据");
    }

    /**
     * 获取在线记录详细信息
     */
    @GetMapping(value = "/{zxId}")
    public AjaxResult getInfo(@PathVariable("zxId") Long zxId)
    {
        return AjaxResult.success(dzZaixianService.selectDzZaixianByZxId(zxId));
    }

    /**
     * 新增在线记录
     */
    @PostMapping
    public AjaxResult add(@RequestBody DzZaixian dzZaixian)
    {
        //先获取今天有没有已经登录但是还未退出的记录，有就不新增
        dzZaixian.setShichang1("2");
        dzZaixian.setZxDate(new Date());
        List<DzZaixian> list = dzZaixianService.selectDzZaixianListWeb(dzZaixian);
        if(list!=null&&list.size()>0){
            return toAjax(1);
        }else{
            UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            dzZaixian.setIp(ip);
            dzZaixian.setLaiyuan(userAgent.getBrowser().getName());
            return toAjax(dzZaixianService.insertDzZaixian(dzZaixian));
        }
    }

    /**
     * 修改在线记录
     */
    @PutMapping
    public AjaxResult edit(@RequestBody DzZaixian dzZaixian)
    {
        //获取今天为结束的数据
        dzZaixian.setShichang1("2");
        dzZaixian.setZxDate(new Date());
        List<DzZaixian> list = dzZaixianService.selectDzZaixianListWeb(dzZaixian);
        if(list!=null&&list.size()>0){
            DzZaixian temp = list.get(0);
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long xztime = System.currentTimeMillis();
            System.out.println("当前时间"+format.format(new Date()));
            long zxTIme = temp.getZxDate().getTime();
            System.out.println("登录时间"+format.format(temp.getZxDate()));
            long diff=(xztime-zxTIme)/1000/60;//获取两个时间相差的分钟
            if(diff<=0){
                diff=1l;
            }
            temp.setShichang(diff+"");
            return toAjax(dzZaixianService.updateDzZaixian(temp));
        }else{
            return toAjax(1);
        }



    }

    /**
     * 删除在线记录
     */
    @Log(title = "在线记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{zxIds}")
    public AjaxResult remove(@PathVariable Long[] zxIds)
    {
        return toAjax(dzZaixianService.deleteDzZaixianByZxIds(zxIds));
    }
}
